package com.znf4.controller.notice;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Random;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.async.DeferredResult;
import org.springframework.web.multipart.MultipartFile;
import com.znf4.base.BaseReturnModel;
import com.znf4.constants.CommonConst;
import com.znf4.enums.OperationStatusEnum;
import com.znf4.id.IdGenerator;
import com.znf4.notice.entity.Notice;
import com.znf4.notice.enums.NoticeStatusEnum;
import com.znf4.notice.server.NoticeServer;
import com.znf4.utils.OSSFile;

/**
 * 活动和公告
 */
@RestController
@RequestMapping("/notice")
public class NoticeController {
	
	private static final Logger LOG = LoggerFactory.getLogger(NoticeController.class);
	
	@Autowired
	private NoticeServer noticeServer ;
	
	@Autowired
	private IdGenerator idGenerator ;
	
	private static DeferredResult<String> deferredResult  ;  // 公告信息
	

	/**
	 * 函数功能说明 ： 添加活动、公告
	 *
	 * @参数： @return
	 * @return String
	 * @throws
	 */
	@RequestMapping(value = "/add", method = { RequestMethod.POST, RequestMethod.GET })
	public Map<String, Object> add(Notice notice , @RequestParam(value = "file", required = false)List<MultipartFile> files ) {
		
		try {
			notice.setId(idGenerator.getActiveNameNumber());
			String fileName = CommonConst.ACTIVE_IMG_NUMBER + notice.getId();
			// 文件上传OSS
			for (MultipartFile multipartFile : files) {
				String orignalFileName = multipartFile.getOriginalFilename();
				if (multipartFile != null && orignalFileName != null && orignalFileName.length() > 0) {
					notice.setImg(OSSFile.upFile(multipartFile, fileName));
					LOG.info("活动、公告上传图片到oss.....并设置数据库参数。");
				}
			}
			noticeServer.addNotice(notice);
		} catch (Exception e) {
			LOG.info("添加活动信息异常 :" , e.getMessage());
			return BaseReturnModel.getMapModel("faild", "", "系统异常");
		}
		return BaseReturnModel.getMapModel("success", "操作完成", "");
	}

	
	
	/**
	 * 函数功能说明 ： 修改公告、活动
	 *
	 * @参数： @return
	 * @return String
	 * @throws
	 */
	@RequestMapping(value = "/update", method = { RequestMethod.POST, RequestMethod.GET })
	public Map<String, Object> update(Notice notice , @RequestParam(value = "file", required = false)List<MultipartFile> files ) {

		try {
			String fileName = CommonConst.ACTIVE_IMG_NUMBER + notice.getId();
			// 文件上传OSS
			for (MultipartFile multipartFile : files) {
				String orignalFileName = multipartFile.getOriginalFilename();
				if (multipartFile != null && orignalFileName != null && orignalFileName.length() > 0) {
					notice.setImg(OSSFile.upFile(multipartFile, fileName));
					LOG.info("活动、公告上传图片到oss.....并设置数据库参数。");
				}
			}
			noticeServer.updateNotice(notice);
		} catch (Exception e) {
			LOG.info("修改活动信息异常 :", e.getMessage());
			return BaseReturnModel.getMapModel("faild", "", OperationStatusEnum.ERROR.getDesc());
		}
		return BaseReturnModel.getMapModel("success", "操作完成", "");
	}
	
	

	/**
	 * 函数功能说明 ： 删除广告
	 *
	 * @参数： @return
	 * @return String
	 * @throws
	 */
	@RequestMapping(value = "/delOne", method = { RequestMethod.POST, RequestMethod.GET })
	public Map<String, Object> delOne(Notice notice) {
		
		try {
			noticeServer.delOneNotice(notice);
			// 删除文件 OSS
			OSSFile.deleteFile(notice.getImg());
		} catch (Exception e) {
			LOG.info("删除单个广告信息异常 :", e.getMessage());
			return BaseReturnModel.getMapModel("faild", "", OperationStatusEnum.ERROR.getDesc());
		}
		return BaseReturnModel.getMapModel("success", "操作完成", "");
	}
	
	
	/**
	 * 函数功能说明 ： 删除多条农作物商品
	 *
	 * @参数： @return
	 * @return String
	 * @throws
	 */
	@RequestMapping(value = "/delAll", method = { RequestMethod.POST, RequestMethod.GET })
	public Map<String, Object> delAll(String[] ids) {
		try {
			List<Notice> lNotices = new ArrayList<>();
			for (int i = 0; i < ids.length; i++) {

				Notice notice = new Notice();
				notice.setId(ids[i]);
				lNotices.add(notice);
				// 删除文件
				Notice delNotice = noticeServer.queryOne(notice);
				OSSFile.deleteFile(delNotice.getImg());
			}
			noticeServer.delAllNotice(lNotices); 
		} catch (Exception e) {
			LOG.info("删除多个其他商品信息异常:", e.getMessage());
			return BaseReturnModel.getMapModel("faild", "", OperationStatusEnum.ERROR.getDesc());
		}
		return BaseReturnModel.getMapModel("success", "操作完成", "");
	}

	
	
	/**
	 * 函数功能说明 ： 获取所有活动/公告
	 *
	 * @参数： @return @return String @throws
	 */
	@RequestMapping(value = "/queryAll", method = { RequestMethod.POST, RequestMethod.GET })
	public Map<String, Object> queryAll(Notice notice ) {
		List<Notice> list = noticeServer.queryAll(notice);
		return BaseReturnModel.getMapModel("success", list, "");
	}
	
	
	
	
	
	
	
	
	
	
	
	/**
	 * 函数功能说明 ： 获取一条公告推送
	 * 
	 * @return
	 */
	@RequestMapping(value = "/queryNotice" , produces = "text/heml;charset=UTF-8")
	@ResponseBody
	public DeferredResult<String> queryNotice(){
	
		System.out.println("开始");
		deferredResult = new DeferredResult<String>() ;
		System.out.println("输出");
		
		return  deferredResult ;
	} 
	
	/**
	 * 定时推送
	 */
//	@Scheduled(fixedDelay = 5000 )
	public void reFresh(){
		
		LOG.info("开始循环");		
		if(deferredResult != null){
			
	        LOG.info("不为null");		
	        Notice notice = new Notice() ;
	        notice.setStatus(NoticeStatusEnum.ACTIVE.getDesc());
			/**
			 * 去数据库中查
			 */
	        noticeServer.queryOne(notice) ;
	        Random r = new Random() ;
			deferredResult.setResult("这是一条公告 :" + r.nextInt()) ;
		}
		
		
	}
	
	
	
	
	
	
	
	

}
